<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>智慧农业文章中心</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            line-height: 1.6;
            color: #333;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }

        .header {
            text-align: center;
            margin-bottom: 40px;
            color: white;
        }

        .header h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
        }

        .header p {
            font-size: 1.1rem;
            opacity: 0.9;
        }

        .loading {
            text-align: center;
            padding: 40px;
            color: white;
            font-size: 1.2rem;
        }

        .loading::after {
            content: '';
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid rgba(255,255,255,0.3);
            border-radius: 50%;
            border-top-color: white;
            animation: spin 1s ease-in-out infinite;
            margin-left: 10px;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }

        .articles-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
            gap: 25px;
            margin-top: 30px;
        }

        .article-card {
            background: white;
            border-radius: 15px;
            padding: 25px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
        }

        .article-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            background: linear-gradient(90deg, #667eea, #764ba2);
        }

        .article-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.15);
        }

        .article-id {
            display: inline-block;
            background: linear-gradient(135deg, #667eea, #764ba2);
            color: white;
            padding: 5px 12px;
            border-radius: 20px;
            font-size: 0.8rem;
            font-weight: 600;
            margin-bottom: 15px;
        }

        .article-title {
            font-size: 1.3rem;
            font-weight: 600;
            margin-bottom: 15px;
            color: #2c3e50;
            line-height: 1.4;
        }

        .article-preview {
            color: #666;
            font-size: 0.95rem;
            line-height: 1.6;
            margin-bottom: 20px;
            display: -webkit-box;
            -webkit-line-clamp: 3;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }

        .article-actions {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .read-more-btn {
            background: linear-gradient(135deg, #667eea, #764ba2);
            color: white;
            text-decoration: none;
            padding: 10px 20px;
            border-radius: 25px;
            font-weight: 500;
            transition: all 0.3s ease;
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }

        .read-more-btn:hover {
            transform: translateX(3px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }

        .read-more-btn::after {
            content: '→';
            transition: transform 0.3s ease;
        }

        .read-more-btn:hover::after {
            transform: translateX(3px);
        }

        .error-message {
            background: #ff6b6b;
            color: white;
            padding: 20px;
            border-radius: 10px;
            text-align: center;
            margin: 20px 0;
        }

        .retry-btn {
            background: white;
            color: #ff6b6b;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            margin-top: 10px;
            font-weight: 500;
        }

        .retry-btn:hover {
            background: #f8f9fa;
        }

        .stats {
            display: flex;
            justify-content: center;
            gap: 30px;
            margin-bottom: 30px;
            flex-wrap: wrap;
        }

        .stat-item {
            background: rgba(255,255,255,0.1);
            padding: 15px 25px;
            border-radius: 10px;
            text-align: center;
            color: white;
            backdrop-filter: blur(10px);
        }

        .stat-number {
            font-size: 1.8rem;
            font-weight: 700;
            display: block;
        }

        .stat-label {
            font-size: 0.9rem;
            opacity: 0.8;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .container {
                padding: 15px;
            }

            .header h1 {
                font-size: 2rem;
            }

            .articles-grid {
                grid-template-columns: 1fr;
                gap: 20px;
            }

            .article-card {
                padding: 20px;
            }

            .stats {
                gap: 15px;
            }

            .stat-item {
                padding: 10px 15px;
            }
        }

        @media (max-width: 480px) {
            .header h1 {
                font-size: 1.5rem;
            }

            .article-title {
                font-size: 1.1rem;
            }

            .article-actions {
                flex-direction: column;
                gap: 10px;
                align-items: stretch;
            }

            .read-more-btn {
                text-align: center;
                justify-content: center;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header class="header">
            <h1>🌱 智慧农业文章中心</h1>
            <p>探索现代农业技术，推动可持续发展</p>
        </header>

        <div class="stats" id="stats" style="display: none;">
            <div class="stat-item">
                <span class="stat-number" id="articleCount">0</span>
                <span class="stat-label">篇文章</span>
            </div>
            <div class="stat-item">
                <span class="stat-number">100%</span>
                <span class="stat-label">原创内容</span>
            </div>
        </div>

        <div class="loading" id="loading">
            正在加载文章列表
        </div>

        <div class="error-message" id="error" style="display: none;">
            <h3>加载失败</h3>
            <p>无法获取文章列表，请检查网络连接</p>
            <button class="retry-btn" onclick="loadArticles()">重试</button>
        </div>

        <div class="articles-grid" id="articlesGrid"></div>
    </div>

    <script>
        let articles = [];

        function showLoading() {
            document.getElementById('loading').style.display = 'block';
            document.getElementById('error').style.display = 'none';
            document.getElementById('articlesGrid').innerHTML = '';
            document.getElementById('stats').style.display = 'none';
        }

        function hideLoading() {
            document.getElementById('loading').style.display = 'none';
        }

        function showError() {
            document.getElementById('error').style.display = 'block';
            hideLoading();
        }

        function showStats(count) {
            document.getElementById('articleCount').textContent = count;
            document.getElementById('stats').style.display = 'flex';
        }

        function truncateText(text, maxLength = 150) {
            if (text.length <= maxLength) return text;
            return text.substring(0, maxLength) + '...';
        }

        function renderArticles(articles) {
            const grid = document.getElementById('articlesGrid');
            grid.innerHTML = '';

            articles.forEach((article, index) => {
                const card = document.createElement('div');
                card.className = 'article-card';
                card.style.animationDelay = `${index * 0.1}s`;
                
                card.innerHTML = `
                    <div class="article-id">文章 #${article.id}</div>
                    <h2 class="article-title">${article.title}</h2>
                    <p class="article-preview">${truncateText(article.content)}</p>
                    <div class="article-actions">
                        <a href="articleDetail.html?id=${article.id}" class="read-more-btn">
                            阅读全文
                        </a>
                    </div>
                `;
                
                grid.appendChild(card);
            });

            // 添加入场动画
            const cards = grid.querySelectorAll('.article-card');
            cards.forEach((card, index) => {
                setTimeout(() => {
                    card.style.opacity = '0';
                    card.style.transform = 'translateY(20px)';
                    card.style.transition = 'all 0.5s ease';
                    
                    requestAnimationFrame(() => {
                        card.style.opacity = '1';
                        card.style.transform = 'translateY(0)';
                    });
                }, index * 100);
            });
        }

        async function loadArticles() {
            showLoading();
            
            try {
                const response = await fetch('/api/articles');
                
                if (!response.ok) {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }
                
                const data = await response.json();
                
                if (Array.isArray(data) && data.length > 0) {
                    articles = data;
                    hideLoading();
                    renderArticles(articles);
                    showStats(articles.length);
                } else {
                    throw new Error('没有找到文章数据');
                }
                
            } catch (error) {
                console.error('加载文章失败:', error);
                showError();
            }
        }

        // 页面加载完成后自动加载文章
        document.addEventListener('DOMContentLoaded', loadArticles);

        // 添加键盘导航支持
        document.addEventListener('keydown', function(e) {
            if (e.key === 'r' || e.key === 'R') {
                if (e.ctrlKey || e.metaKey) {
                    e.preventDefault();
                    loadArticles();
                }
            }
        });
    </script>
</body>
</html>
